package com.hskn.hss.core.utils;

import com.alibaba.fastjson.JSON;
import com.hskn.hss.core.utils.spring.SpringUtils;
import com.hskn.hss.core.utils.uuid.ServletUtils;
import com.hskn.hss.module.button.entity.SysButton;
import com.hskn.hss.module.menu.entity.SysMenu;
import com.hskn.hss.module.operatedauthor.service.impl.SysRoleMenuServiceImpl;
import com.hskn.hss.module.role.entity.SysRole;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.service.impl.UserServiceImpl;
import com.hskn.hss.utils.GroupingByUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class LoginUtils {
    /**
     * 登录用户
     *
     * @return
     */
    public static User getUser() {
        HttpServletRequest request = ServletUtils.getRequest();
        User user = null;
        if (null != request.getSession().getAttribute("userInfo")) {
            user = JSON.parseObject(request.getSession().getAttribute("userInfo").toString(), User.class);
        }
        return user;
    }

    /**
     * 用户角色
     *
     * @param loginUser
     * @return
     */
    public static List<SysRole> getLoginRoleList(User loginUser) {
        List<SysRole> roleList = SpringUtils.getBean(UserServiceImpl.class).getSysRoleByUId(loginUser.getUid());
        return roleList;
    }

    /**
     * 用户按钮权限
     *
     * @param sysRoleList
     * @return
     */
    public static Set<String> getPermissions(List<SysRole> sysRoleList) {
        List<String> roleIds = sysRoleList.stream().map(SysRole::getId).collect(Collectors.toList());
        List<SysButton> sysButtonList = SpringUtils.getBean(SysRoleMenuServiceImpl.class).getButtonByRoleIds(roleIds);
        List<SysMenu> sysMenuList = SpringUtils.getBean(SysRoleMenuServiceImpl.class).getMenuListByRoleIds(roleIds);
        Map<String, SysMenu> sysMenuMap = GroupingByUtils.getGroupingForMapEntity(sysMenuList, SysMenu::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        Set<String> result = new HashSet<>();
        for (SysButton sysButton : sysButtonList) {
            if (sysMenuMap.containsKey(sysButton.getMenuid())) {
                result.add(sysMenuMap.get(sysButton.getMenuid()).getAction() + "/" + sysButton.getLabel());
            }
        }
        return result;
    }

    /**
     * @return 用户身份1：超管，2：管理员 3：用户
     */
    public static Integer getLoginUserIdentity() {
        User loginUser = getUser();
        List<SysRole> roleList = getLoginRoleList(loginUser);
        Boolean isAdmin = false;
        for (SysRole sysRole : roleList) {
            if(sysRole.getId().equals("00000000")){
                return 1;
            }
            if(sysRole.getRoleType().equals(1)){
                isAdmin = true;
            }
        }
        if (isAdmin) {
            return 2;
        } else {
            return 3;
        }
    }
}
